<?php
/*
Copyright (C) 2009 de Flotte Maxence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
?>
<?php 
/**
 * Permet de traduire des expressions à partir d'un dictionnaire
 */
class ZzTranslation {
	private $_tranlation;
	private $file;
/**
 * Constructor
 * @param string $file Dictionnary name
 * @param Bout $obj don't use
 * @return void
 */
	public function __construct($file,$obj = null) {
		$this->file = $file;
		$genFile = './ZzT/gen/'.$file.'_'.$_SESSION['lang'].'.gen.lang';
		$translationFile = './ZzT/'.$file.'_'.$_SESSION['lang'].'.lang';
		$defaultGenFile = './ZzT/gen/'.$file.'_default.gen.lang';
		$defaultTranslationFile = './ZzT/'.$file.'_default.lang';

		if(!isSet($GLOBALS['translation_'.$file])) {
			if(!isSet($_SESSION['lang'])) 
				$_SESSION['lang'] = 'default';

			if(file_exists($genFile)) {
				$this->loadGenFile($genFile);
			}
			elseif(file_exists($translationFile)) {
				$this->mkGenFile($translationFile,$genFile);
			}
			elseif(file_exists($defaultGenFile)) {
				$this->loadGenFile($defaultGenFile);
			}
			elseif(file_exists($defaultTranslationFile)) {
				$this->mkGenFile($defaultTranslationFile,$defaultGenFile);
			}
			else { }
			$GLOBALS['translation_'.$file] = $this->_translation;
		}
		else {
			$this->_translation = $GLOBALS['translation_'.$file];
		}
	}
/**
 * Translate a sentence
 * @param string $str Sentence
 * @param int $option Nb of elements
 * @return string
 */
	public function tr($str,$option = 0) {
		if(isSet($this->_translation[$str])) {
			$_option = $this->_translation[$str][1];
			if($option < sizeof($_option)-1)
				$option = $_option[$option];
			else
				$option = $_option[sizeof($_option)-1];
			return str_replace('()',$option,$this->_translation[$str][0]);
		}
		else
			return $str;
	}
/**
 * Make a Gen File
 * @param string $translationFile The dictionary file
 * @param string $genFile The gen file
 */
	public function mkGenFile($translationFile,$genFile) {
		$dico = file_get_contents($translationFile);
		foreach(explode("\n",$dico) as $key => $line) {
			$line = explode('|',$line);
			$this->_translation[$line[0]][] = preg_replace('!\(.*\)!','()',$line[1]);
			$this->_translation[$line[0]][] = explode(',', preg_replace('!^.*\((.*)\).*$!','$1',$line[1]));
		}
		file_put_contents($genFile,serialize($this->_translation));
	}
/**
 * Load a Gen File
 * @param string $genFile The gen file
 */
	public function loadGenFile($genFile) {
		$this->_translation = unserialize(file_get_contents($genFile));
	}
}
?>